import {
  userLogin,
  userLogout
} from '@/api/user/index.js';
const user = {
  state: {
    // 用户认证token
    token: '',
    // 用户邮箱
    email: ''
  },

  mutations: {
    SET_TOKEN: (state, value) => {
      state.token = value
    },
    SET_EMAIL: (state, value) => {
      state.email = value
    }
  },

  actions: {
    /* 用户登录 (输入邮箱和密码) */
    async Login({
      commit,
      rootState
    }, data) {
      const res = await userLogin(data)
      const {
        token,
        email
      } = res.data
      commit('SET_IS_LOGIN', true, {
        root: true
      })
      commit('SET_TOKEN', token)
      commit('SET_EMAIL', email)
      uni.setStorageSync('is_login', true);
      uni.setStorageSync('user_info', res.data);

    },
    /* 用户退出登录 */
    async Logout({
      commit,
      rootState
    }) {
      const res = await userLogout()
      commit('SET_IS_LOGIN', false, {
        root: true
      })
      commit('SET_TOKEN', '')
      commit('SET_EMAIL', '')
      uni.removeStorageSync('is_login');
      uni.removeStorageSync('user_info');

    },

  }
}

export default user